package com.hotel.customersystem.listener;

import com.hotel.config.websocket.WebSocketProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private WebSocketProcess webSocketProcess;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    @Override
    public void onMessage(Message message, byte[] pattern) {
        String keyStr = message.toString();
        if(keyStr.startsWith("unPayedOrder")){
            Integer userId = Integer.valueOf( keyStr.split("_")[1]);
            String orderId =String.valueOf( keyStr.split("_")[2]);
            System.out.println("订单id =" + orderId + "刚到失效时间");
            String msg = "您有一个订单"+orderId+"尚未支付，还有5分钟时间，请尽快支付哦！";
            try {
                webSocketProcess.sendMessage(String.valueOf(userId),msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("信息:{}",msg);
        }
    }
}
